<?php

/**
 * This is the model class for table "doitac_khachhang".
 *
 * The followings are the available columns in table 'doitac_khachhang':
 * @property integer $id
 * @property integer $id_khachhang
 * @property integer $id_doitac
 * @property string $ms_khachhang
 * @property string $so_phieu_thanh_toan
 * @property integer $so_tien
 * @property integer $chiet_khau
 * @property integer $ngay_tao
 * @property integer $trangthai_thanhtoan
 */
class DoitacKhachhang extends CActiveRecord
{
	public $trangthai_thanhtoan;
	public $ma_so_doi_tac;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'doitac_khachhang';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_khachhang, id_doitac, ms_khachhang, so_phieu_thanh_toan, so_tien, chiet_khau, chi_phi_dv, ngay_tao', 'required'),
			array('id_khachhang, id_doitac, so_tien, chiet_khau, ngay_tao, trangthai_thanhtoan', 'numerical', 'integerOnly'=>true),
			array('ms_khachhang, so_phieu_thanh_toan', 'length', 'max'=>50),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, id_khachhang, id_doitac,ngay_thanh_toan_dt,ngay_thanh_toan_kh, ms_khachhang, so_phieu_thanh_toan, so_tien, chiet_khau, ngay_tao, trangthai_thanhtoan_kh,trangthai_thanhtoan_dt,chung_tu_thanh_toan', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'doitac'=> array(self::BELONGS_TO,'Doitac','id_doitac'),
			'khachhang'=> array(self::BELONGS_TO,'Khachhang','id_khachhang'),
		);
	}
	public static function TrangThaiDoitac($value)
	{
		return $value->trangthai_thanhtoan_dt;
	}
	public static function getTrangThai_Doitac($value)
	{
		if (DoitacKhachhang::TrangThaiDoitac($value) == 0) {
			return "Chưa thanh toán";
		}else if (DoitacKhachhang::TrangThaiDoitac($value) == 1) {
			return "Đã thanh toán một phần";
		}else{
			return "Đã thanh toán";
		}
	}
	public static function TrangThaiKhachHang($value)
	{
		return $value->trangthai_thanhtoan_kh;
	}
	public static function getTrangThai_KhachHang($value)
	{
		if (DoitacKhachhang::TrangThaiKhachHang($value) == 0) {
			return "Chưa thanh toán";
		}else if (DoitacKhachhang::TrangThaiKhachHang($value) == 1) {
			return "Đã thanh toán một phần";
		}else{
			return "Đã thanh toán";
		}
	}
	public static function getTrangThaiThanhToan($value)
	{
		if ($value->trangthai_thanhtoan == 0) {
			return "Chưa thanh toán";
		}else if ($value->trangthai_thanhtoan == 1) {
			return "Đã thanh toán một phần";
		}else{
			return "Đã thanh toán";
		}
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_khachhang' => 'Thành viên',
			'id_doitac' => 'Đối tác',
			'ms_khachhang' => 'Mã số thành viên',
			'so_phieu_thanh_toan' => 'Số phiếu thanh toán',
			'so_tien' => 'Số tiền',
			'chiet_khau' => 'Chiết khấu',
			'ngay_tao' => 'Ngày tạo',
			'chua_thanh_toan_dt' => 'Chưa thanh toán',
			'chua_thanh_toan_kh' => 'Chưa thanh toán',
			'trangthai_thanhtoan_kh' => 'Trạng thái',
			'trangthai_thanhtoan_dt' => 'Trạng thái',
			'chung_tu_thanh_toan' =>"Chứng từ thanh toán",
			'ma_so_doi_tac' =>"Mã số đối tác",
			'ngay_su_dung' =>"Ngày sử dụng",
			'ngay_thanh_toan_dt'=>"Ngày thanh toán",
			'ngay_thanh_toan_kh'=>"Ngày thanh toán",
		);
	}
	public static function createExcelDoiTac($model,$list)
	{
		if ($model != null) {
				error_reporting(E_ALL);
				ini_set('display_errors', TRUE);
				ini_set('display_startup_errors', TRUE);
				Yii::import('ext.Excel.XPHPExcel');		
				$objPHPExcel = XPHPExcel::createPHPExcel();
				$objPHPExcel->getProperties()->setCreator("Trieu nhu")
											 ->setLastModifiedBy("Trieu nhu")
											 ->setTitle("Trieu nhu")
											 ->setSubject("Trieu nhu")
											 ->setDescription("Trieu nhu.")
											 ->setKeywords("Trieu nhu")
											 ->setCategory("Trieu nhu");
				$objPHPExcel->getActiveSheet()->setCellValue('A1',"Triết xuất hóa đơn của đối tác {$model->ten}");
				$objPHPExcel->getActiveSheet()->setCellValue('A3',"Tổng doanh thu");
				$objPHPExcel->getActiveSheet()->setCellValue('B3',$model->tong_doanh_thu);
				$objPHPExcel->getActiveSheet()->setCellValue('A4',"Chi phí");
				$objPHPExcel->getActiveSheet()->setCellValue('B4',$model->tong_chi_phi);
				$objPHPExcel->getActiveSheet()->setCellValue('A5',"Đã thanh toán");
				$objPHPExcel->getActiveSheet()->setCellValue('B5',$model->da_thanh_toan);
				$objPHPExcel->getActiveSheet()->setCellValue('A6',"Chưa thanh toán");
				$objPHPExcel->getActiveSheet()->setCellValue('B6',$model->chua_thanh_toan);
				$i = 7;
				$objPHPExcel->setActiveSheetIndex(0)
				            ->setCellValue("A".$i, 'Id')
				            ->setCellValue('B'.$i, 'Tên khách hàng')
				            ->setCellValue('C'.$i, 'Mã khách hàng')
				            ->setCellValue('D'.$i, "Số phiếu thanh toán")
				            ->setCellValue('E'.$i, "Số tiền")
				            ->setCellValue('F'.$i, "Chiết khấu")
				            ->setCellValue('G'.$i, "Chưa thanh toán")
				            ->setCellValue('H'.$i, 'Trạng thái thanh toán')
				            ->setCellValue('I'.$i, "Thời điểm thanh toán")
				            ->setCellValue('J'.$i, "Chứng từ thanh toán");
				foreach ($list as $key ) {
					$i++;
					$trangthai = DoitacKhachhang::getTrangThai_Doitac($key);
					$objPHPExcel->setActiveSheetIndex(0)
				            ->setCellValue("A".$i, $key->id)
				            ->setCellValue('B'.$i, $key->khachhang->ten)
				            ->setCellValue('C'.$i, $key->ms_khachhang)
				            ->setCellValue('D'.$i, $key->so_phieu_thanh_toan)
				            ->setCellValue('E'.$i, getMoney($key->so_tien))
				            ->setCellValue('F'.$i, getMoney($key->chiet_khau))
				            ->setCellValue('G'.$i, getMoney($key->chua_thanh_toan_dt))
				            ->setCellValue('H'.$i, $trangthai)
				            ->setCellValue('I'.$i, date("H:i m/d/Y",strtotime($key->ngay_tao)))
				            ->setCellValue('J'.$i, $key->chung_tu_thanh_toan);
				}
						// Set active sheet index to the first sheet, so Excel opens this as the first sheet
				$objPHPExcel->setActiveSheetIndex(0);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('A')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('B')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('D')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('G')
						    ->setWidth(20);		    
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('H')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('C')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('I')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('J')
						    ->setWidth(20);
				// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
				$filename = toSlug(stripVietnamese($model->ten)).'-'.strtotime(date("Y-m-d H:i:s")).'.xls';
				header('Content-Type: application/vnd.ms-excel');
				header("Content-Disposition: attachment;filename={$filename}");
				header('Cache-Control: max-age=0');

				$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
				$objWriter->save('php://output');
			// return $file;
		}
	}
	public static function createExcelKhachHang($model,$list)
	{
		if ($model != null) {
				error_reporting(E_ALL);
				ini_set('display_errors', TRUE);
				ini_set('display_startup_errors', TRUE);
				Yii::import('ext.Excel.XPHPExcel');		
				$objPHPExcel = XPHPExcel::createPHPExcel();
				$objPHPExcel->getProperties()->setCreator("Trieu nhu")
											 ->setLastModifiedBy("Trieu nhu")
											 ->setTitle("Trieu nhu")
											 ->setSubject("Trieu nhu")
											 ->setDescription("Trieu nhu.")
											 ->setKeywords("Trieu nhu")
											 ->setCategory("Trieu nhu");
				$objPHPExcel->getActiveSheet()->setCellValue('A1',"Triết xuất hóa đơn của thành viên {$model->ten}");
				$objPHPExcel->getActiveSheet()->setCellValue('A3',"Tổng tiêu dùng");
				$objPHPExcel->getActiveSheet()->setCellValue('B3',$model->tong_tieu_dung);
				$objPHPExcel->getActiveSheet()->setCellValue('A4',"Chiết khấu");
				$objPHPExcel->getActiveSheet()->setCellValue('B4',$model->duoc_chiet_khau);
				$objPHPExcel->getActiveSheet()->setCellValue('A5',"Đã thanh toán");
				$objPHPExcel->getActiveSheet()->setCellValue('B5',$model->da_thanh_toan);
				$objPHPExcel->getActiveSheet()->setCellValue('A6',"Chưa thanh toán");
				$objPHPExcel->getActiveSheet()->setCellValue('B6',$model->chua_thanh_toan);
				$i = 7;
				$objPHPExcel->setActiveSheetIndex(0)
				            ->setCellValue("A".$i, 'Id')
				            ->setCellValue('B'.$i, 'Tên đối tác')
				            ->setCellValue('C'.$i, "Số phiếu thanh toán")
				            ->setCellValue('D'.$i, "Số tiền")
				            ->setCellValue('E'.$i, "Chiết khấu")
				            ->setCellValue('F'.$i, "Chưa thanh toán")
				            ->setCellValue('G'.$i, 'Trạng thái thanh toán')
				            ->setCellValue('H'.$i, "Thời điểm thanh toán")
				            ->setCellValue('I'.$i, "Chứng từ thanh toán");
				foreach ($list as $key ) {
					$i++;
					$trangthai = DoitacKhachhang::getTrangThai_KhachHang($key);
					$objPHPExcel->setActiveSheetIndex(0)
				            ->setCellValue("A".$i, $key->id)
				            ->setCellValue('B'.$i, $key->doitac->ten)
				            ->setCellValue('C'.$i, $key->so_phieu_thanh_toan)
				            ->setCellValue('D'.$i, getMoney($key->so_tien))
				            ->setCellValue('E'.$i, getMoney($key->chiet_khau))
				            ->setCellValue('F'.$i, getMoney($key->chua_thanh_toan_kh))
				            ->setCellValue('G'.$i, $trangthai)
				            ->setCellValue('H'.$i, date("H:i m/d/Y",strtotime($key->ngay_tao)))
				            ->setCellValue('I'.$i, $key->chung_tu_thanh_toan);
				}
						// Set active sheet index to the first sheet, so Excel opens this as the first sheet
				$objPHPExcel->setActiveSheetIndex(0);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('A')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('B')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('D')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('G')
						    ->setWidth(20);		    
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('H')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('C')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('I')
						    ->setWidth(20);

				// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
				$filename = toSlug(stripVietnamese($model->ten)).'-'.strtotime(date("Y-m-d H:i:s")).'.xls';
				header('Content-Type: application/vnd.ms-excel');
				header("Content-Disposition: attachment;filename={$filename}");
				header('Cache-Control: max-age=0');

				$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
				$objWriter->save('php://output');
			// return $file;
		}
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search($condition = '')
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->order ="id desc";
		if (isset($_GET['date1']) && isset($_GET['date2'])) {
			$date1 = date('Y-m-d',strtotime($_GET['date1']));
			$date2 = date('Y-m-d',strtotime($_GET['date2']));
			if ($condition == '') {
				$condition = "ngay_su_dung >= '$date1' and ngay_su_dung <= '$date2'";
			}else{
				$condition .= " and ngay_su_dung >= '$date1' and ngay_su_dung <= '$date2'";
			}
		}
		$criteria->condition = $condition;
		$criteria->compare('id',$this->id);
		$criteria->compare('id_khachhang',$this->id_khachhang);
		$criteria->compare('id_doitac',$this->id_doitac);
		$criteria->compare('ms_khachhang',$this->ms_khachhang,true);
		$criteria->compare('so_phieu_thanh_toan',$this->so_phieu_thanh_toan,true);
		$criteria->compare('so_tien',$this->so_tien);
		$criteria->compare('chiet_khau',$this->chiet_khau);
		$criteria->compare('ngay_tao',$this->ngay_tao);
		$criteria->compare('chung_tu_thanh_toan',$this->chung_tu_thanh_toan);
		$criteria->compare('trangthai_thanhtoan_kh',$this->trangthai_thanhtoan);
		$criteria->compare('trangthai_thanhtoan_dt',$this->trangthai_thanhtoan);
		$criteria->compare('ngay_thanh_toan_kh',$this->ngay_thanh_toan_kh);
		$criteria->compare('ngay_thanh_toan_dt',$this->ngay_thanh_toan_dt);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
            	'pageSize'=>30,
            ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return DoitacKhachhang the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
